/**
 * @Author: XXHH
 * @CreateTime: 2024-12-11
 */
public class Demo23 {
    public static void main(String[] args) throws InterruptedException {
        Object locker = new Object();
        Object locker2 = new Object();
        Thread t1 = new Thread(()->{
            // 先拿起 醋
            synchronized (locker){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                // 再拿起 酱油
                synchronized (locker2){
                    System.out.println("t1 同时拿到两个锁");
                }
            }
        });
        Thread t2 = new Thread(()->{
            // 先拿起 醋
            synchronized (locker){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                // 在拿起 酱油
                synchronized (locker2){
                    System.out.println("t2 同时拿到两个锁...");
                }
            }
        });


        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }
}
